package fr.upmc.ta.android;

import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class HelloWorldActivity extends Activity {

	/**la date et heure*/
	private Date time;
	private String stime = "";
	private static TextView viewDate;

	/**Debbug*/
	private static String TAG="HelloWorldActivity";
	private static String KEY="HWdate";

	public HelloWorldActivity () {
		super();
		time = new Date();
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main) ;

		viewDate = (TextView) findViewById(R.id.idHeure);
		viewDate.setOnCreateContextMenuListener(this);

		String format = "dd/MM/yy H:mm:ss";

		java.text.SimpleDateFormat formater = new java.text.SimpleDateFormat( format );
		//time = new java.util.Date();

		stime = formater.format( time );
		System.out.println("date du jour : " +stime); 

		//viewDate.setText(viewDate.getText() + stime);


		/* gestion du cliques bouton time */
		Button btTime = (Button)findViewById(R.id.idTime);
		btTime.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				viewDate.setText(viewDate.getText()+"\n" +stime);
			}
		});

		/* gestion du clique bouton cliear */
		Button btClear = (Button) findViewById(R.id.idClear);
		btClear.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				viewDate.setText("Hello, la date du jour est : " + "");
			}
		});


	}



	/**********************
	 * 
	 ***********************/
	@Override
	protected void onStart() {
		super.onStart();
		Log.d(TAG, "Demarrage du l'appli");
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		Log.d(TAG, "Redemarrage de l'appli");
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.d(TAG, "Resume de l'appli");
	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.d(TAG, "Pause de l'appli");
	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.e(TAG, "Stop de l'appli");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.e(TAG, "Destruction de l'appli");
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);

		outState.putCharSequence(KEY, viewDate.getText());
		Log.i(TAG, "HelloWorld: onSaveInstanceState");
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);

		CharSequence sequece = savedInstanceState.getCharSequence(KEY);
		viewDate.setText(sequece);
		Log.i(TAG, "HelloWorld: onRestoreInstanceState");
	}


	/* gestion des menus */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		//Intent menuToast = new Intent(this, MenuToast.class);
		getMenuInflater().inflate(R.menu.menu, menu);
		Log.i(TAG, "onCreateOptionsMenu");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.itemGreen:
			viewDate.setTextColor(Color.GREEN);
			break;
		case R.id.itemRed:
			viewDate.setTextColor(Color.RED);
			break;
		default:
			break;
		}
		Log.i(TAG, "onOptionsItemSelected");
		return super.onOptionsItemSelected(item);
	}

	/* gestion des menus contextuelle */
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {

		getMenuInflater().inflate(R.menu.menu, menu);
		getMenuInflater().inflate(R.menu.contextmenu, menu);

		Log.i(TAG,"onCreateContextMenu");
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		Context context = getApplicationContext();
		CharSequence text = "";
		Toast toast = null;

		switch (item.getItemId()) {
		case R.id.itemCopy:
			text = "Copy ...";
			toast  = Toast.makeText(context, text, Toast.LENGTH_LONG);
			toast.show();
			break;

		case R.id.itemPaste:
			text = "Past ...";
			toast  = Toast.makeText(context, text, Toast.LENGTH_LONG);
			toast.show();
			break;
		case R.id.itemGreen:
			viewDate.setTextColor(Color.GREEN);
			break;
		case R.id.itemRed:
			viewDate.setTextColor(Color.RED);
			break;

		default:
			Log.i(TAG, "onContextItemSelected");
			break;
		}

		return super.onContextItemSelected(item);
	}


}